package com.moinut.studentattendancemanager.controller;

import com.moinut.studentattendancemanager.controller.callback.OnGetListListener;
import com.moinut.studentattendancemanager.controller.callback.OnGetListener;
import com.moinut.studentattendancemanager.controller.callback.OnUpdateListener;
import com.moinut.studentattendancemanager.model.factory.DAOFactory;
import com.moinut.studentattendancemanager.model.vo.TeacherInfo;
import com.moinut.studentattendancemanager.model.vo.db.Teacher;
import com.sun.istack.internal.NotNull;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author MOILING
 */
public class TeacherController {

    public static void updateTeacherEmail(@NotNull String teacherId, @NotNull String newEmail, @NotNull OnUpdateListener listener) {
        if (newEmail == null) {
            return;
        }
        if (newEmail.isEmpty()) {
            listener.onError("新邮箱不得为空");
            return;
        }
        Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(newEmail);
        if (!matcher.matches()) {
            listener.onError("新邮箱格式不对");
            return;
        }
        try {
            if (DAOFactory.getITeacherDAOInstance().updateTeacherEmail(teacherId, newEmail)) {
                listener.onSuccess("修改资料成功");
            } else {
                listener.onError("修改资料失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void getAllTeachers(@NotNull OnGetListListener<Teacher> listener) {
        try {
            List<Teacher> teachers = DAOFactory.getITeacherDAOInstance().getAllTeachers();
            if (teachers.size() > 0) {
                listener.onSuccess(teachers);
            } else {
                listener.onError("没找到任何的教师");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void getAllTeachersInfo(@NotNull OnGetListListener<TeacherInfo> listener) {
        try {
            List<TeacherInfo> teachers = DAOFactory.getITeacherInfoDAOInstance().getAllTeachersInfo();
            if (teachers.size() > 0) {
                listener.onSuccess(teachers);
            } else {
                listener.onError("没找到任何的教师");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }


    public static void getTeacherById(@NotNull String teacherId, @NotNull OnGetListener<Teacher> listener) {
        try {
            Teacher teacher = DAOFactory.getITeacherDAOInstance().getTeacherById(teacherId);
            if (teacher != null) {
                listener.onSuccess(teacher);
            } else {
                listener.onError("没找到该教师");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void updateTeacherInfo(@NotNull String oldId, @NotNull Teacher newTeacher, @NotNull OnUpdateListener listener) {
        Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(newTeacher.getTeacherEmail());
        if (!matcher.matches()) {
            listener.onError("新邮箱格式不对");
            return;
        }
        try {
            if (DAOFactory.getITeacherDAOInstance().updateTeacherInfo(oldId, newTeacher)) {
                listener.onSuccess("修改资料成功");
            } else {
                listener.onError("修改资料失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }
}
